package model

import (
	"fmt"
	"gorm.io/gorm"
	"log"
)

type SchoolNew struct {
	Admissions           string `json:"admissions" gorm:"type:varchar(255); default:null"`
	Answerurl            string `json:"answerurl" gorm:"type:varchar(255); default:null"`
	Belong               string `json:"belong" gorm:"type:varchar(255); default:null"`
	Central              string `json:"central" gorm:"type:varchar(255); default:null"`
	CityID               string `json:"city_id" gorm:"type:varchar(255); default:null"`
	CityName             string `json:"city_name" gorm:"type:varchar(255); default:null"`
	CodeEnroll           string `json:"code_enroll" gorm:"type:varchar(255); default:null"`
	CollegesLevel        string `json:"colleges_level" gorm:"type:varchar(255); default:null"`
	CountyID             string `json:"county_id" gorm:"type:varchar(255); default:null"`
	CountyName           string `json:"county_name" gorm:"type:varchar(255); default:null"`
	Department           string `json:"department" gorm:"type:varchar(255); default:null"`
	Doublehigh           string `json:"doublehigh" gorm:"type:varchar(255); default:null"`
	DualClass            string `json:"dual_class" gorm:"type:varchar(255); default:null"`
	DualClassName        string `json:"dual_class_name" gorm:"type:varchar(255); default:null"`
	F211                 string `json:"f211" gorm:"type:varchar(255); default:null"`
	F985                 string `json:"f985" gorm:"type:varchar(255); default:null"`
	Hightitle            string `json:"hightitle" gorm:"type:varchar(255); default:null"`
	InnerRate            string `json:"inner_rate" gorm:"type:varchar(255); default:null"`
	IsRecruitment        string `json:"is_recruitment" gorm:"type:varchar(255); default:null"`
	Level                string `json:"level" gorm:"type:varchar(255); default:null"`
	LevelName            string `json:"level_name" gorm:"type:varchar(255); default:null"`
	Name                 string `json:"name" gorm:"type:varchar(255); default:null"`
	Nature               string `json:"nature" gorm:"type:varchar(255); default:null"`
	NatureName           string `json:"nature_name" gorm:"type:varchar(255); default:null"`
	OuterRate            string `json:"outer_rate" gorm:"type:varchar(255); default:null"`
	ProvinceID           string `json:"province_id" gorm:"type:varchar(255); default:null"`
	ProvinceName         string `json:"province_name" gorm:"type:varchar(255); default:null"`
	Rank                 string `json:"rank" gorm:"type:varchar(255); default:null"`
	RankType             string `json:"rank_type" gorm:"type:varchar(255); default:null"`
	Rate                 string `json:"rate" gorm:"type:varchar(255); default:null"`
	RecommendMasterLevel string `json:"recommend_master_level" gorm:"type:varchar(255); default:null"`
	SchoolID             string `json:"school_id" gorm:"type:varchar(255); default:null"`
	SchoolType           string `json:"school_type" gorm:"type:varchar(255); default:null"`
	TagName              string `json:"tag_name" gorm:"type:varchar(255); default:null"`
	TownName             string `json:"town_name" gorm:"type:varchar(255); default:null"`
	Type                 string `json:"type" gorm:"type:varchar(255); default:null"`
	TypeName             string `json:"type_name" gorm:"type:varchar(255); default:null"`
	UpgradingLevel       string `json:"upgrading_level" gorm:"type:varchar(255); default:null"`
	ViewMonth            string `json:"view_month" gorm:"type:varchar(255); default:null"`
	ViewTotal            string `json:"view_total" gorm:"type:varchar(255); default:null"`
	ViewTotalNumber      string `json:"view_total_number" gorm:"type:varchar(255); default:null"`
	ViewWeek             string `json:"view_week" gorm:"type:varchar(255); default:null"`
	CollageAvatar        string `json:"collage_avatar" gorm:"type:varchar(255);default:null"`
	gorm.Model
}

type SchoolIDs struct {
	SchoolID string `json:"school_id" gorm:"type:varchar(255); default:null"`
}

func FirstByNameNew(where SchoolNew) (first SchoolNew) {
	db.Where(&where, "name", "code_enroll").First(&first)
	return
}

func CreateNew(school SchoolNew) {
	db.Create(&school)
}

func UpdateNew(school SchoolNew) {
	db.Where(&school, "name", "code_enroll").Updates(school)
}

func UpdateOrCreateNew(school SchoolNew) {
	isExist := FirstByNameNew(school)
	if len(isExist.Name) == 0 {
		fmt.Println("创建....................................")
		CreateNew(school)
	} else {
		fmt.Println("修改....................................")
		UpdateNew(school)
	}
}

func RowsDeal(rowDeal func(row *SchoolNew)) {
	rows, err := db.Model(&SchoolNew{}).Order("id asc").Rows()
	if err != nil {
		log.Fatalln("获取记录失败：", err)
	}
	defer rows.Close()
	for rows.Next() {
		var schoolNew SchoolNew
		err := db.ScanRows(rows, &schoolNew)
		if err != nil {
			log.Fatalln("获取行错误：", err)
		}
		rowDeal(&schoolNew)
	}

}
func TotalRows() (total int64) {
	db.Model(&SchoolNew{}).Count(&total)
	return
}
